<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          服务
        </h2>
      </div>
    </div>
  </div>
</div>
<!-- 业务页面代码 -->
{% if Count > 0 %}
<div class="page-body">
  <div class="container-xl">
    <div class="row row-deck row-cards">
      <div class="col-12">
        <div class="row row-cards">
          {% for Scheduler in SchedulerTasks %}
          <div class="col-12 col-md-6 col-xl-3">
            <div class="card card-sm card-link-pop">
              <div class="card-body">
                <div class="row align-items-center">
                  <div class="col-auto">
                    <span class="bg-{{ Scheduler.color }} text-white avatar">
                      {{ Scheduler.svg|safe }}
                    </span>
                  </div>
                  <div class="col">
                    <div class="font-weight-medium">
                      <a href="javascript:show_service_modal('{{ Scheduler.id }}','{{ Scheduler.name }}')"
                        id="service_btn">{{ Scheduler.name }}</a>
                    </div>
                    <div class="text-muted">
                      {{ Scheduler.time }}
                    </div>
                  </div>
                  {% if Scheduler.state == "ON" %}
                  <div class="col-auto align-self-center">
                    <div class="badge bg-primary"></div>
                  </div>
                  {% endif %}
                </div>
              </div>
            </div>
          </div>
          {% endfor %}
        </div>
      </div>
    </div>
  </div>
</div>
{% else %}
<div class="page-body">
  <div class="container-xl d-flex flex-column justify-content-center">
    <div class="empty">
      <div class="empty-img"><img src="./static/img/sign_in.svg" height="128" alt="">
      </div>
      <p class="empty-title">没有服务</p>
      <p class="empty-subtitle text-muted">
        没有开启任何后台服务。
      </p>
    </div>
  </div>
</div>
{% endif %}
<div class="modal modal-blur fade" id="modal-logging" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">实时日志</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"
          id="logging_close_head"></button>
      </div>
      <div class="modal-body" id="logging_content" style="display:flex; flex-direction:column-reverse;font-size:12px;">
      </div>
      <div class="modal-footer">
        <a href="javascript:pause_logging()" class="btn btn-link link-secondary me-auto" id="logging_stop_btn">
          暂停
        </a>
        <button type="button" class="btn btn-primary" data-bs-dismiss="modal" id="logging_close_btn">关闭</button>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-nametest" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title">名称识别测试</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col">
              <div class="mb-3">
                <label class="form-label">资源名称 <span class="form-help bg-red-lt" title="用于测试名称识别情况，如命名太离谱实在难于兼容，合理范围内无法识别的可在Github反馈" data-bs-toggle="tooltip">?</span></label>
                <input type="text" value="" id="test_name" class="form-control" placeholder="种子名/文件名等">
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="mb-3" id="test_result">
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-white me-auto" data-bs-dismiss="modal">取消</button>
        <button id="nametest_btn" class="btn btn-primary">识别</button>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-ruletest" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title">过滤规则测试</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-lg-8">
              <div class="mb-3">
                <label class="form-label required">标题</label>
                <input type="text" value="" id="test_title" class="form-control" placeholder="种子名称">
              </div>
            </div>
            <div class="col-lg-4">
              <div class="mb-3">
                <label class="form-label required">大小(GB)</label>
                <input type="text" value="" id="test_size" class="form-control" placeholder="种子大小">
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="mb-3">
                <label class="form-label">副标题</label>
                <textarea rows="3" id="test_subtitle" class="form-control" placeholder="种子描述"></textarea>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="mb-3" id="testrule_result">
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-white me-auto" data-bs-dismiss="modal">取消</button>
        <button id="ruletest_btn" class="btn btn-primary">测试</button>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">

  //刷新LOG标志
  var refresh_logging_flag = false
  //开始刷新日志
  function start_logging() {
    refresh_logging_flag = true;
    refresh_logging();
  }
  //停止刷新日志
  function stop_logging() {
    refresh_logging_flag = false;
  }
  //刷新日志
  function refresh_logging(flag) {
    ajax_post("logging", {}, function (ret) {
      if (ret.text) {
        $("#logging_content").append(ret.text);
        var height = $('#logging_content').prop("scrollHeight");
        $('#logging_content').scrollTop(height);
      }
      if($("#modal-logging").is(":hidden") && flag){
        stop_logging();
      }
      if (refresh_logging_flag == true) {
        setTimeout("refresh_logging(true)", 2000);
      }
    });
  }

  // 显示服务提示框
  function show_service_modal(id, name) {
    if (id == "logging") {
      $('#modal-logging').modal('show');
      start_logging();
    } else if (id == "nametest") {
      $('#modal-nametest').modal('show');
    } else if (id == "ruletest") {
      $('#modal-ruletest').modal('show');
    } else if (id == "blacklist"){
      show_confirm_modal("清理缓存后，已转移过的文件允许重新转移（包括识别错误的文件），是否确认？", function(){
        hide_confirm_modal();
        ajax_post("truncate_blacklist", {}, function (ret) {
          show_success_modal("文件缓存清理完成！");
        });
      });
    } else {
      show_ask_modal("是否立即运行 " + name + "？", function () {
        hide_ask_modal();
        run_scheduler(id, name);
      });
    }
  }

  // 运行服务
  function run_scheduler(id, name) {
    var data = { "item": id };
    ajax_post("sch", data, function (ret) {
      show_success_modal(name + " 服务启动成功，正在后台运行");
    });
  }

  //暂停实时日志
  function pause_logging() {
    if (refresh_logging_flag == true) {
      stop_logging();
      $("#logging_stop_btn").text("开始")
    } else {
      start_logging();
      $("#logging_stop_btn").text("暂停")
    }
  }

  //实时日志关闭
  $("#logging_close_btn").click(function () {
    stop_logging();
  });

  $("#logging_close_head").click(function () {
    stop_logging();
  });

  $("#nametest_btn").click(function () {
    name_test();
  });

  $("#ruletest_btn").click(function () {
    rule_test();
  });

  //名称识别测试
  function name_test(){
    var name = $("#test_name").val();
    if(!name){
      $("#test_name").addClass("is-invalid");
      return;
    }else{
      $("#test_name").removeClass("is-invalid");
    }
    $("#nametest_btn").text("识别中");
    $("#nametest_btn").attr("disabled", true);
    ajax_post("name_test", {"name": name}, function (ret) {
      $("#nametest_btn").text("识别");
      $("#nametest_btn").attr("disabled", false);
      if(ret.code == 0){
        $("#test_result").empty();
        if(ret.data.type){
          $("#test_result").append('<span class="badge badge-outline text-blue me-1 mb-1" title="类型">' + ret.data.type + '</span>');
        }
        if(ret.data.category){
          $("#test_result").append('<span class="badge badge-outline text-blue me-1 mb-1" title="类别">' + ret.data.category + '</span>');
        }
        if(ret.data.name){
          $("#test_result").append('<span class="badge badge-outline text-red me-1 mb-1" title="识别名称">' + ret.data.name + '</span>');
        }
        if(ret.data.title){
          $("#test_result").append('<span class="badge badge-outline text-red me-1 mb-1" title="标题">' + ret.data.title + '</span>');
        }
        if(ret.data.year){
          $("#test_result").append('<span class="badge badge-outline text-orange me-1 mb-1" title="年份">' + ret.data.year + '</span>');
        }
        if(ret.data.season_episode){
          $("#test_result").append('<span class="badge badge-outline text-orange me-1 mb-1" title="季集">' + ret.data.season_episode + '</span>');
        }
        if(ret.data.tmdbid){
          $("#test_result").append('<span class="badge badge-outline text-green me-1 mb-1" title="TMDB ID">' + ret.data.tmdbid + '</span>');
        }
        if(ret.data.restype){
          $("#test_result").append('<span class="badge me-1 mb-1" title="质量">' + ret.data.restype + '</span>');
        }
        if(ret.data.pix){
          $("#test_result").append('<span class="badge me-1 mb-1" title="分辨率">' + ret.data.pix + '</span>');
        }
        if(ret.data.video_codec){
          $("#test_result").append('<span class="badge me-1 mb-1" title="视频编码">' + ret.data.video_codec + '</span>');
        }
        if(ret.data.audio_codec){
          $("#test_result").append('<span class="badge me-1 mb-1" title="音频编码">' + ret.data.audio_codec + '</span>');
        }
      }
    });
  }

  //过滤规则测试
  function rule_test(){
    var title = $("#test_title").val();
    if(!title){
      $("#test_title").addClass("is-invalid");
      return;
    }else{
      $("#test_title").removeClass("is-invalid");
    }
    var size = $("#test_size").val();
    if(!size || isNaN(size)){
      $("#test_size").addClass("is-invalid");
      return;
    }else{
      $("#test_size").removeClass("is-invalid");
    }
    var subtitle = $("#test_subtitle").val();
    $("#ruletest_btn").text("测试中");
    $("#ruletest_btn").attr("disabled", true);
    ajax_post("rule_test", {"title": title, "subtitle": subtitle, "size": size}, function (ret) {
      $("#ruletest_btn").text("测试");
      $("#ruletest_btn").attr("disabled", false);
      if(ret.code == 0){
        $("#testrule_result").empty();
        if(ret.flag){
          $("#testrule_result").append('<span class="badge bg-green me-1 mb-1">'+ret.text+'</span>');
          $("#testrule_result").append('<span class="badge badge-outline text-orange me-1 mb-1" title="命中规则组">'+ret.name+'</span>');
          $("#testrule_result").append('<span class="badge badge-outline text-blue me-1 mb-1" title="命中规则在规则组内的序号">优先级：'+ret.order+'</span>');
        }else{
          $("#testrule_result").append('<span class="badge bg-red me-1 mb-1">'+ret.text+'</span>');
        }
      }
    });
  }

</script>